#!/usr/bin/env python
#-*- coding:utf-8 -*-
from wwpy.wuba.service.IMC import IMC
from wwpy.io.Source import Source
from wwpy.util.Threads import ThreadPool
from wwpy.util.Time import Time
from wwpy.util.Wrapper import looper
import json,os

imc=IMC()
imc.start()

class InfoAttr(object):
    
    def __init__(self,date=Time.yesterday(),threads=4,path='.'):
        self.date=Time.date_format(date)
        self.mini_date=Time.date_format(date,'')
        self._threads=threads
        self.info_attr_file='%s%sinfo_attr_%s' % (path,os.sep,self.mini_date)
        self._sep='|'
        self._num=8
        self._bld={}
        self._bcd={}
        self._dld={}
        self._dcd={}
        
    def get_info_ids(self,idx):
        sql="select distinct entity_id from db_bid_promotion.creative_info_%s where entity_type=0 and date(add_time)='%s' and entity_id!='' and entity_id is not null " %(idx,self.date)
        s=Source(sql)
        info_ids=dict([(row[0],idx) for row in s.read()])
        return info_ids
    
    def get_info(self,info_id):
        return imc.get_info(info_id)
        
    def _get_dict(self,sql):
        data_dict={}
        s=Source(sql)
        for row in s.read():
            data_dict[row[0]]=row[1:]
        return data_dict
        
    def get_belong_cate_dict(self):
        sql="select cate_id,cate1,cate2 from db_base_dict.belong_cate"
        return self._get_dict(sql)
    
    def get_display_cate_dict(self):
        sql="select list_name,cate_id,cate1,cate2,cate3,cate4 from db_base_dict.display_cate"
        return self._get_dict(sql)
    
    def get_belong_local_dict(self):
        sql="select local_id,city1,city2,city3 from db_base_dict.belong_local"
        return self._get_dict(sql)
    
    def get_display_local_dict(self):
        sql="select local_id,city1,city2,city3 from db_base_dict.display_local"
        return self._get_dict(sql)
    
    def _parse_locals(self,locals):
        parser=json.loads(locals)
        ld=[]
        for p in parser:
            l=str(p.get(u'L',0))
            d=str(p.get(u'D',0))
            ld.append((l,d))
        return ld
    
    def _parse_url(self,url):
        return url.replace("http://","").strip("/").split("/")[-1]
    
    def parse_info(self,info):
        locals=info.getLocals()
        belong_cate=info.getCateID()
        url=info.getUrl()
        #params=info.getParams()
        state=info.getState()
        url=info.getUrl()
        return [locals,belong_cate,url,state]
    
    def do_task(self,info_ids):
        infos=[]
        for info_id in info_ids:
            if not info_id:
                continue
            try:
                info=self.get_info(info_id)
            except Exception,e:
                print e
                info=None
                continue
            if info is None:
                continue
            row=self.parse_info(info)
            infos.append([info_id]+row)
        return infos
    
    def get_idx_infos(self):
        tp=ThreadPool(self._threads)
        for idx in range(self._num):
            tp.put(self.get_info_ids,idx)
        tp.wait()
        info_ids={}
        for out in tp.outs():
            info_ids.update(out[-1])
        idx_infos={}
        for info_id,idx in info_ids.iteritems():
            idx_infos.setdefault(idx,[]).append(info_id)
        return idx_infos
    
    def run(self):
        base_info=self.get_base_info()
        self.switch_base_info(base_info)
	self.load()
        
    def get_data_dicts(self):
        self._bld=self.get_belong_local_dict()
        self._dld=self.get_display_local_dict()
        self._bcd=self.get_belong_cate_dict()
        self._dcd=self.get_display_cate_dict()
        
    def switch_base_info(self,base_info):
        self.get_data_dicts()
        f=open(self.info_attr_file,'w')
        for row in base_info:
            new_row=self.transform(row)
            f.write(self._sep.join(str(field) for field in new_row)+'\n')
        f.close()
        
    def get_base_info(self):
        idx_infos=self.get_idx_infos()
        results=[]
        for idx,info_ids in idx_infos.iteritems():
            res=self.do_task(info_ids)
            results.extend(res)
        return results
    
    def transform(self,row):
        new_row=[]
        info_id,locals,belong_cate_id,url,state=row
        belong_cates=self._bcd.get(str(belong_cate_id),('',''))
        ld=self._parse_locals(locals)
        belong_local_id,display_local_id=ld[0]
        belong_locals=self._bld.get(belong_local_id,map(lambda idx:'',range(3)))
        display_locals=self._dld.get(display_local_id,map(lambda idx:'',range(3)))
        if len(ld)==1:
            has_cities=0
            has_locals=0
        else:
            has_cities=0
            has_locals=1
            city1=display_locals[0]
            for l,d in ld[1:]:
                dls=self._dld.get(d,map(lambda idx:'',range(3)))
                if dls[0]!=city1:
                    has_cities=1
                    break
        display_cates=self._dcd.get(self._parse_url(url) or '',map(lambda idx:'',range(5)))
        new_row.extend([info_id,has_cities,has_locals,belong_local_id,display_local_id])
        new_row.extend(belong_locals)
        new_row.extend(display_locals)
        new_row.extend([belong_cate_id,display_cates[0]])
        new_row.extend(belong_cates)
        new_row.extend(display_cates[1:])
        new_row.extend([url,state])
        return new_row
    
    def load(self):
        table_fields=[
            "info_id bigint(20) comment '信息ID'",
            "has_cities smallint(2) comment '是否多城市 1是 0否'",
            "has_locals smallint(2) comment '是否多商圈 1是 0否'",
            "belong_local_id bigint(20) comment '归属地域ID'",
            "display_local_id bigint(20) comment '表现地域ID'",
            "belong_city1 varchar(30) comment '归属一级城市'",
            "belong_city2 varchar(30) comment '归属二级城市'",
            "belong_city3 varchar(30) comment '归属三级城市'",
            "display_city1 varchar(30) comment '表现一级城市'",
            "display_city2 varchar(30) comment '表现二级城市'",
            "display_city3 varchar(30) comment '表现三级城市'",
            "belong_cate_id bigint(20) comment '归属类别ID'",
            "display_cate_id bigint(20) comment '表现类别ID'",
            "belong_cate1 varchar(30) comment '归属一级类别'",
            "belong_cate2 varchar(30) comment '归属二级类别'",
            "display_cate1 varchar(30) comment '表现一级类别'",
            "display_cate2 varchar(30) comment '表现二级类别'",
            "display_cate3 varchar(30) comment '表现三级类别'",
            "display_cate4 varchar(30) comment '表现四级类别'",
            "url varchar(200) comment '表现路径'",
            "state int(10) comment '信息状态'",
        ]
        from wwpy.db.DBI import DBI
        dbi=DBI(dbc='db_precise_ib')
        dbi.save(self.info_attr_file,'info_attr',table_fields,sep=self._sep,is_update=False,is_check=False,options={'ENGINE':'BRIGHTHOUSE'})
        dbi.close()
    
@looper('DAY')
def main(date):
    ia=InfoAttr(date,path='/work2/opt/analytic/precise/ads/info_attr')
    ia.run()
    
if __name__ == '__main__':
    main()
    
